<?php
/**
 * 语言句子类
 */


class Sentence extends MyBase{
	
	function __construct() {
		parent::__construct('sentence.id', array('content' => 'content', 'mark' => 'mark'));
	}
	
	/**
	 * 修改语言句子
	 */
	public function edit($post, $replace = false){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('langid' => $post['langid'], 'libraryid' => $post['libraryid'], 'mark' => $post['mark']);
		if($id > 0){
			$select['kept'] = " AND id!=$id"; 
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){
			if($replace){
				$id = $rs['id'];
			}else{
				return lang('already').lang('exist');
			}
		}
		$post['state'] = isset($post['state']) ? $post['state'] : 0;
		$data = array(
			'langid'	=> $post['langid'],
			'libraryid'	=> $post['libraryid'],
			'mark'		=> $post['mark'],
			'content'	=> $post['content'],
			'state'		=> $post['state'],
		);
		if(is_numeric($id) && $id > 0){
			$data['updatetime'] = TIME;
			return $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			return $this->db->insert($this->tbname, $data);
		}
	}
	
	/**
	 * 修改语言句子-多语言同时编辑
	 */
	public function multiEdit($post){
		$iRtn = 0;
		foreach($post['content'] as $key => $content){
			if(empty($content))continue;
			list($langid, $libraryid, $mark, $id) = explode('-', $key);
			if($id > 0){
				$rs = $this->db->get_row($this->tbname, array('langid' => $langid, 'libraryid' => $libraryid, 'mark' => $mark, 'kept' => " AND id!=$id"), 'id');
				if(!empty($rs))continue;
				$data = array(
					'libraryid'	=> $post['libraryid'],
					'mark'		=> $post['mark'],
					'content'	=> $content,
					'updatetime'=> TIME 
				);
				if($this->db->update($this->tbname, array('id' => $id), $data)){
					$iRtn ++;
				}
			}else{
				$rs = $this->db->get_row($this->tbname, array('langid' => $langid, 'libraryid' => $libraryid, 'mark' => $mark,), 'id');
				if(empty($rs)){
					$data = array(
						'langid'	=> $langid,
						'libraryid'	=> $post['libraryid'],
						'mark'		=> $post['mark'],
						'content'	=> $content,
						'state'		=> 1,
						'addtime'	=> TIME
					);
					if($this->db->insert($this->tbname, $data)){
						$iRtn ++;
					}
				}else{
					$data = array(
						'libraryid'	=> $post['libraryid'],
						'mark'		=> $post['mark'],
						'content'	=> $content,
						'updatetime'=> TIME 
					);
					if($this->db->update($this->tbname, array('id' => $rs['id'],), $data)){
						$iRtn ++;
					}
				}
			}
		}
		return $iRtn;
	}
			
	/**
	 * 删除语言句子
	 */
	/*public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		foreach($post['list'] as $id){
			if($this->db->delete($this->tbname, array('id' => $id))){
				$iRtn ++;
			}
		}
		return $iRtn;
	}*/
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		if(!empty($select['content'])){
			$select['kept'] .= " AND `content` LIKE '%{$select['content']}%'";
			unset($select['content']);
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/*public function get_all($fields = '*', $select = array(), $order = array()){
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}*/
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_lang(){
		$list = $this->get_all();
		if(!empty($list['list'])){
			$lang_list = array();
			$lang_id_libraryid_list = array();
			foreach($list['list'] as $lang){
				$lang_list[$lang['id']] = $lang;
				$lang_id_libraryid_list[$lang['id']] = $lang['libraryid'];
			}
			return cache_write('langs.php', array('lang_list' => $lang_list, 'lang_id_libraryid_list' => $lang_id_libraryid_list));
		}
		return -1;
	}
	
	// 生成菜单语言文件	
	public function export_menu_sentences(){
		$_path = DAT_PTH.'lang/zh-cn/';
		$_pre = 'gm_menu';
		$list = $this->get_all('mark,content', array('langid' => 1, 'libraryid' => 4));
		if(empty($list['list'])){
			$data_php = array('There is no sentence in this module.');
			$data_js = array("empty:'There is no sentence in this module.'");
		}else{
			$data_php = array();
			$data_js = array();
			foreach($list['list'] as $row){
				$data_php[$row['mark']] = $row['content'];
				$data_js[] = "'{$row['mark']}':'{$row['content']}'";
			}
		}
		
		$_file_php = $_pre.'.lang.php';
		cache_write($_file_php, $data_php, $_path);
		
		// 清除可能存在的EA缓存
		$key = md5("{$_path}{$_file_php}");
		ea_clear($key);
		
		$_file_js = $_path."{$_pre}.lang.js";
		$data_js = "var _lang = {\n".(implode(",\n", $data_js))."\n};\n$.extend(_l, _lang);";
		File::write_file($_file_js, $data_js);
		return true;
	}
	
}
?>